﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
using WebSysCore.JSons;
using WebSysCore;
using WebSysCore.Helpers;
using HSchSchedule.Utils;

namespace HSchSchedule.AjaxPages
{
    public partial class AjaxAddSessionLession : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod(true)]

        public static string AddSessionLession(List<JSession> lstJSession, List<JLession> lstJLession)
        {
            int cusId;
            int.TryParse(HttpContext.Current.Session[GUIConstants.CUSTOMERID_SESSION].ToString(), out cusId);
            //check xem Day đưa lên có đúng định dạng ko?
            if (cusId > 0)
            {
                bool ok = true;
                string err = "", err2 = "";
                foreach (JSession js in lstJSession)
                {
                    if (!ValidateHelp.ValidateSession(js, out err))
                    {
                        ok = false;
                        break;
                    }

                }
                foreach (JLession jl in lstJLession)
                {
                    if (!ValidateHelp.ValidateLession(jl, out err2))
                    {
                        ok = false;
                        break;
                    }
                }
                if (ok)
                {
                    int schId;
                    int.TryParse(HttpContext.Current.Session[GUIConstants.CURRENTSCHEDULE_SESSION].ToString(), out schId);
                    if (schId > 0)
                    {
                        List<Session> lstSession = new List<Session>();
                        lstSession.Add(new Session()
                        {
                            ScheduleInListId = schId,
                            SessionName = "All",
                            Comment = "Hệ thống tự thêm.",
                            Order = 0
                        });
                        for (int i = 0; i < lstJSession.Count; i++)
                        {
                            Session s = AssignHelp.JSessionToSession(lstJSession[i]);
                            s.ScheduleInListId = schId;
                            lstSession.Add(s);
                        }
                        //DataBase.InsertSessioṇ̣̣̣
                        DataBase.InsertSession(lstSession);
                        List<Lession> lstLession = new List<Lession>();
                        for (int i = 0; i < lstJLession.Count; i++)
                        {
                            Lession l = AssignHelp.JLessionToLession(lstJLession[i]);
                            l.ScheduleInListId = schId;
                            lstLession.Add(l);
                        }
                        DataBase.InsertLession(lstLession);
                        return GUIConstants.SUCCESS_RESULT;
                    }
                    else
                    {
                        return GUIConstants.FAIL_RESULT;
                    }
                }
                else
                {
                    return GUIConstants.FAIL_RESULT;
                }
            }
            else
            {
                return GUIConstants.SESSIONCLOSE_RESULT;
            }

        }
    }
}
